package com.softaria.windows.model;

import com.allen_sauer.gwt.dnd.client.util.WidgetLocation;
import com.softaria.windows.widgets.Frame;

/**
 * Implementation of {@link PositionCalculator} which shifts each next Frame bit
 * to the right and to the bottom, relatively to previous one.
 * 
 * @author Roman M Kuzmin (roman@softaria.com)
 * 
 */
public class CascadePositionCalculator implements PositionCalculator {

  private static final int STEP = 25;

  private Frame currentFrame;

  private class PositionImpl implements Position {
    private int x;
    private int y;

    public PositionImpl(int x, int y) {
      super();
      this.x = x;
      this.y = y;
    }

    public int getLeft() {
      return x;
    }

    public int getTop() {
      return y;
    }

    public PositionImpl shift(int width, int height) {
      int newX = x + STEP;
      int newY = y + STEP;
      if (newX >= width) {
        newX = 0;
      }
      if (newY >= height) {
        newY = 0;
      }
      return new PositionImpl(newX, newY);
    }
  }

  public CascadePositionCalculator(Frame currentFrame) {
    this.currentFrame = currentFrame;
  }

  public Position calculatePosition(int width, int height) {

    PositionImpl currentPosition = null;

    if (currentFrame != null) {
      WidgetLocation loc = currentFrame.getLocation();
      currentPosition = new PositionImpl(loc.getLeft(), loc.getTop());

    } else {
      currentPosition = new PositionImpl(0, 0);
    }

    currentPosition = currentPosition.shift(width, height);

    return currentPosition;

  }

}
